#include "input.h"

#include <boost/foreach.hpp>
#define foreach BOOST_FOREACH
#ifdef DEBUG
#include <iostream>
#endif
#include <fstream>

namespace Tujchi {

Input::Input(std::string _file) : file(_file) { }

Input::~Input(void) { }

std::vector< std::vector<Vector> > Input::parse() const {
#ifdef DEBUG
	int count = 0;
#endif

	std::vector< std::vector<Vector> > all_points;
	int number = 0;
	int id = 0;
	double x, y, z;
	std::ifstream fin(file.c_str());
	while(fin >> number) {
		std::vector<Vector> points;
		while(number--) {
#ifdef DEBUG
			count++;
#endif
			fin >> id >> x >> y >> z;
			points.push_back(Vector(x,y));
		}
		all_points.push_back(points);
	}
#ifdef DEBUG
	std::cout << count << " pontos processados" << std::endl;
#endif
	return all_points;	
}

void Input::output(std::string _filename, std::vector<Vector> convex_hull) const {
	std::ofstream fout(_filename.c_str());
	int id = 0;
	fout << convex_hull.size() << std::endl;
	foreach(Vector point, convex_hull) {
		fout << ++id << " " << point.x << " " << point.y << " " << "0" << std::endl;
	}
	id = 0;
	int count = 0;
	fout << convex_hull.size() << std::endl;
	foreach(Vector point, convex_hull) {
		fout << ++id << " 1 1 1 " << count+1 << " " << count+2 << std::endl;
		count++;
		if(count == convex_hull.size()-1) break;
	}
	fout << ++id << " 1 1 1 " << count+1 << " " << "1" << std::endl;


}

}

